
//
//  SYDFeedBackHeaderView.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/14.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "SYDFeedBackHeaderView.h"
#define feedNormalColor [UIColor colorWithHexString:@"#bababa"]
#define feedSelectColor [UIColor colorWithHexString:@"#5298ff"]
#define feedBackColor [UIColor colorWithHexString:@"#f5f8fc"]
@interface SYDFeedBackHeaderView()
@property (weak, nonatomic) IBOutlet UIButton *functionBtn;
@property (weak, nonatomic) IBOutlet UIButton *performanceBtn;
@property (weak, nonatomic) IBOutlet UIButton *ExperienceBtn;
@property (weak, nonatomic) IBOutlet UIButton *otherBtn;
// 按钮数组
@property (nonatomic, strong) NSMutableArray *btnArray;
// 选中按钮
@property (nonatomic, strong) UIButton * tempButton;
@end
@implementation SYDFeedBackHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SYDFeedBackHeaderView class]) owner:nil options:nil].lastObject;
        self.frame = frame;
    }
    return self;
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self.functionBtn drawBorderWithColor:feedNormalColor radius:2];
    [self.performanceBtn drawBorderWithColor:feedNormalColor radius:2];
    [self.ExperienceBtn drawBorderWithColor:feedNormalColor radius:2];
    [self.otherBtn drawBorderWithColor:feedNormalColor radius:2];
    
    [self.functionBtn setTitleColor:GRAY_COLOR forState:UIControlStateNormal];
    [self.performanceBtn setTitleColor:GRAY_COLOR forState:UIControlStateNormal];
    [self.ExperienceBtn setTitleColor:GRAY_COLOR forState:UIControlStateNormal];
    [self.otherBtn setTitleColor:GRAY_COLOR forState:UIControlStateNormal];
    //问题属性 1：功能问题 2：性能问题 3：用户体验 4：其他问题
    self.functionBtn.tag = 0;
    self.performanceBtn.tag = 1;
    self.ExperienceBtn.tag = 2;
    self.otherBtn.tag = 3;
    [self.btnArray addObjectsFromArray:@[self.functionBtn,self.performanceBtn,self.ExperienceBtn,self.otherBtn]];
}

-(void)setDefaultSelect:(FeedBackType)feedbackType{
    switch (feedbackType) {
        case function:
            [self selTag:self.functionBtn];
            break;
        case performance:
            [self selTag:self.performanceBtn];
            break;
        case experience:
            [self selTag:self.ExperienceBtn];
            break;
        case other:
            [self selTag:self.otherBtn];
            break;
    }
}

- (IBAction)selTag:(UIButton *)sender {
    
   NSLog(@"点击了%@", sender.titleLabel.text);
        
        self.tempButton = sender;
        sender.selected = !sender.selected;
        
        for (NSInteger j = 0; j < [self.btnArray count]; j++) {
            UIButton *btn = self.btnArray[j] ;
            if (sender.tag == j) {
                btn.selected = sender.selected;
                [btn setTitleColor:feedSelectColor forState:UIControlStateSelected];
                [btn drawBorderWithColor:feedSelectColor radius:2];
            } else {
                btn.selected = NO;
                [btn setTitleColor:feedNormalColor forState:UIControlStateSelected];
                [btn drawBorderWithColor:feedNormalColor radius:2];
            }
            btn.backgroundColor = feedBackColor;
        }
        
        UIButton *btn = self.btnArray[sender.tag];
        if (btn.selected) {
            btn.backgroundColor = feedBackColor;
            [btn setTitleColor:feedSelectColor forState:UIControlStateSelected];
            [btn drawBorderWithColor:feedSelectColor radius:2];
        } else {
            btn.backgroundColor = feedBackColor;
            [btn setTitleColor:feedNormalColor forState:UIControlStateSelected];
            [btn drawBorderWithColor:feedNormalColor radius:2];
        }
    if (_block) {
        _block(sender.tag + 1);
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)btnArray {
    if (!_btnArray) {
        NSMutableArray *array = [NSMutableArray array];
        _btnArray = array;
        
    }
    return _btnArray;
}
@end

